package org.jeecg.modules.demo.dtwlwjc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglStatisticalData;
import org.jeecg.modules.demo.dtwlwjc.service.IZwglStatisticalDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;

/**
 * @Description: 综合数据
 * @Author: jeecg-boot
 * @Date:   2023-04-17
 * @Version: V1.0
 */
@Api(tags="综合数据")
@RestController
@RequestMapping("/dtwlwjc/zwglStatisticalData")
@Slf4j
public class ZwglStatisticalDataController extends JeecgController<ZwglStatisticalData, IZwglStatisticalDataService> {
	@Autowired
	private IZwglStatisticalDataService zwglStatisticalDataService;
	
	/**
	 * 分页列表查询
	 *
	 * @param zwglStatisticalData
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "综合数据-分页列表查询")
	@ApiOperation(value="综合数据-分页列表查询", notes="综合数据-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZwglStatisticalData>> queryPageList(ZwglStatisticalData zwglStatisticalData,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZwglStatisticalData> queryWrapper = QueryGenerator.initQueryWrapper(zwglStatisticalData, req.getParameterMap());
		Page<ZwglStatisticalData> page = new Page<ZwglStatisticalData>(pageNo, pageSize);
		IPage<ZwglStatisticalData> pageList = zwglStatisticalDataService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param zwglStatisticalData
	 * @return
	 */
	@AutoLog(value = "综合数据-添加")
	@ApiOperation(value="综合数据-添加", notes="综合数据-添加")
	@RequiresPermissions("dtwlwjc:zwgl_statistical_data:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZwglStatisticalData zwglStatisticalData) {
		zwglStatisticalDataService.save(zwglStatisticalData);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param zwglStatisticalData
	 * @return
	 */
	@AutoLog(value = "综合数据-编辑")
	@ApiOperation(value="综合数据-编辑", notes="综合数据-编辑")
	@RequiresPermissions("dtwlwjc:zwgl_statistical_data:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZwglStatisticalData zwglStatisticalData) {
		zwglStatisticalDataService.updateById(zwglStatisticalData);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "综合数据-通过id删除")
	@ApiOperation(value="综合数据-通过id删除", notes="综合数据-通过id删除")
	@RequiresPermissions("dtwlwjc:zwgl_statistical_data:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		zwglStatisticalDataService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "综合数据-批量删除")
	@ApiOperation(value="综合数据-批量删除", notes="综合数据-批量删除")
	@RequiresPermissions("dtwlwjc:zwgl_statistical_data:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zwglStatisticalDataService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "综合数据-通过id查询")
	@ApiOperation(value="综合数据-通过id查询", notes="综合数据-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZwglStatisticalData> queryById(@RequestParam(name="id",required=true) String id) {
		ZwglStatisticalData zwglStatisticalData = zwglStatisticalDataService.getById(id);
		if(zwglStatisticalData==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(zwglStatisticalData);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param zwglStatisticalData
    */
    @RequiresPermissions("dtwlwjc:zwgl_statistical_data:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZwglStatisticalData zwglStatisticalData) {
        return super.exportXls(request, zwglStatisticalData, ZwglStatisticalData.class, "综合数据");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("dtwlwjc:zwgl_statistical_data:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZwglStatisticalData.class);
    }

	 @GetMapping(value = "/getdtcqssl")
	 @Scheduled(fixedRate = 1800000)
	 public Result getmpsj() {
		 QueryWrapper<ZwglStatisticalData> queryWrapper = new QueryWrapper<>();
		 String s = zwglStatisticalDataService.getdtsj();

		 return Result.OK(s);
	 }
	 @GetMapping(value = "/getcqlist")
	 public Result getcqlist(@RequestParam(name = "deviceNo",required=false)String deviceNo){
		return zwglStatisticalDataService.getcqlist(deviceNo);
	 }
	 @GetMapping(value = "/getcqcount")
	 public Result getcqcount(@RequestParam(name = "deviceNo",required=false)String deviceNo){
		 return zwglStatisticalDataService.countCategories();
	 }
	 @GetMapping(value = "/getcqlista")
	 public Result getcqlista(@RequestParam(name = "devict_no",required=false)String deviceNo,@RequestParam(name = "page",required=false)Integer pageNo){
		 return zwglStatisticalDataService.getcqlista(deviceNo, pageNo);
	 }

	 @ApiOperation(value="综合数据-分页列表查询", notes="综合数据-分页列表查询")
	 @GetMapping(value = "/lista")
	 public Result<IPage<ZwglStatisticalData>> queryPageLista(ZwglStatisticalData zwglStatisticalData,
															 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															 @RequestParam(name="pageSize", defaultValue="6") Integer pageSize,
															  @RequestParam(name="deviceNo", required=false) Integer deviceNo,
															 HttpServletRequest req) {
		 QueryWrapper<ZwglStatisticalData> queryWrapper = QueryGenerator.initQueryWrapper(zwglStatisticalData, req.getParameterMap());
		 queryWrapper.eq("devict_no",deviceNo);
		 Page<ZwglStatisticalData> page = new Page<ZwglStatisticalData>(pageNo, pageSize);
		 IPage<ZwglStatisticalData> pageList = zwglStatisticalDataService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }
	 @GetMapping(value = "/getsbbh")
	 public Result getsbbh(){
		 return zwglStatisticalDataService.getcqsbbh();
	 }
	 @GetMapping(value = "/getshijian")
	 public Result getshijian(@RequestParam(name = "kaishishijian",required=false)String kaishishijian,@RequestParam(name = "jieshushijian",required=false)String jieshushijian,@RequestParam(name = "devict_no",required=false)String devictNo){
		 return zwglStatisticalDataService.shijianduancq(kaishishijian,jieshushijian,devictNo);
	 }

	 @GetMapping(value = "/chongqing")
	 public Result chongqing(@RequestParam(name = "starttime", required = false) String starttime,
							 @RequestParam(name = "endtime", required = false) String endtime,
							 @RequestParam(name = "devict_no", required = false) String deviceNo,
							 @RequestParam(name = "page", required = false) Integer pageNo
	 ) {
		 if (starttime=="" || endtime==""){
			 return zwglStatisticalDataService.getcqlista(deviceNo,pageNo);
		 }else {
			 return zwglStatisticalDataService.chongqing(starttime, endtime, deviceNo, pageNo);
		 }

	 }

}
